your caption
The ggplot2 package is a system for data visualization. More information can be found ggplot2 documentation First, we need to install the package and call it.
#install.packages("ggplot2")
library(ggplot2)
We will use the Iris flower data set to explore data visualization with ggplot2. The Iris flower data set is a multivariate data set widely used for statistical and machine learning analysis. Iris flower data set wikipedia
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
plot(x=iris$Sepal.Length, y=iris$Sepal.Width,
xlab="Sepal Length", ylab="Sepal Width", main="Sepal Length-Width")
scatter <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width))
scatter + geom_point(aes(color=Species, shape=Species)) +
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width") + theme_minimal() +theme(plot.background = element_rect(fill = "lightblue"))
To make a graph, we need 3 elements: - a data set - a coordinate system - geoms: visual marks that represent data points, including x, y, size, color, …
ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()
ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(color=Species, shape=Species))
ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(color=Species, shape=Species))+
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width")
ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(color=Species, shape=Species))+
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width") + theme_minimal()
ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(color=Species, shape=Species))+
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width") + theme_minimal() + theme(plot.background = element_rect(fill = "lightblue"))
#install.packages("ggthemes") # Install
library(ggthemes) # Load
ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(color=Species, shape=Species))+
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width") + theme_minimal() + theme_economist()
ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(color=Species, shape=Species))+
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width") + theme_dark() +scale_colour_brewer(palette = "Pastel2")
color=Species into the ggplot initiation function.ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point(aes( shape=Species))+
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width") + theme_minimal() + geom_smooth(method="lm")
## `geom_smooth()` using formula 'y ~ x'
ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point(aes( shape=Species))+
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width") + theme_minimal() + geom_smooth(method="loess")
## `geom_smooth()` using formula 'y ~ x'
ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point(aes( shape=Species))+
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width") + theme_minimal() + geom_smooth(method="loess") + facet_grid(Species ~ .)
## `geom_smooth()` using formula 'y ~ x'
ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point(aes( shape=Species))+
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width") + theme_minimal() + geom_smooth(method="loess") + facet_grid(. ~ Species)
## `geom_smooth()` using formula 'y ~ x'
ggsave("plot.png") #saves the latest plot you make
## Saving 7 x 5 in image
## `geom_smooth()` using formula 'y ~ x'
#or if you have multiple plots and you want to save one of them: use the variable to save it
plot=ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point(aes( shape=Species))+
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width") + theme_bw() + geom_smooth(method="loess") + facet_grid(. ~ Species)
ggsave("plot.png",plot = plot)
## Saving 7 x 5 in image
## `geom_smooth()` using formula 'y ~ x'